<?php
Yii::import('Gallery.models.Photo');
class GalleryHelper
{
    public static function renderThumb($source, $width, $height, $fromFile = true, $filename = null)
    {
        if ($width <= 0)
            $width = 100;
        if ($height <= 0)
            $height = 100;

        include_once(Yii::getPathOfAlias('application.vendors.phpThumb.*') . '/phpthumb.class.php');
        $phpThumb = new phpThumb();
        if ($fromFile) {
            $source = Yii::getPathOfAlias('webroot') . $source;
            if (file_exists($source)) {
                $phpThumb->setSourceData(file_get_contents($source));
                if ($filename == null) {
                    $ext = CFileHelper::getExtension($source);
                    $path = rtrim(Yii::app()->assetManager->basePath, '/') . DIRECTORY_SEPARATOR . 'galleries';
                    if (!file_exists($path))
                        mkdir($path, 0755);
                    $filename = substr(basename($source), 0, -(strlen($ext) + 1)) . '_' . $width . 'x' . $height . '_' . md5($source) . '.' . $ext;
                }
            }
            else
                Yii::log('Not found file :' . $source, CLogger::LEVEL_ERROR);
        }
        else {
            $phpThumb->setSourceData($source);
        }

        $phpThumb->setParameter('w', $width);
        $phpThumb->setParameter('h', $height);
        if ($phpThumb->GenerateThumbnail()) {
            if ($phpThumb->RenderToFile($path . DIRECTORY_SEPARATOR . $filename)) {
                // do something on success
                Yii::trace('Successfully rendered');
                return rtrim(Yii::app()->assetManager->baseUrl, '/') . '/galleries/' . $filename;
            }
            else {
                // do something with debug/error messages
                Yii::log('Failed:<pre>' . implode("\n\n", $phpThumb->debugmessages) . '</pre>', CLogger::LEVEL_ERROR);
                return false;
            }
        }
        else {
            // do something with debug/error messages
            Yii::log('Failed:<pre>' . $phpThumb->fatalerror . "\n\n" . implode("\n\n", $phpThumb->debugmessages) . '</pre>', CLogger::LEVEL_ERROR);
            return false;
        }
    }

    /**
     * get first photo of gallery, if value of $exculude is specificed, mean, this become to get SecondPhoto
     * @static
     * @param $id
     * @param int $exclude
     * @return CActiveRecord|Photo
     */
    public static function getFirstPhoto($id)
    {
        $criteria = new CDbCriteria;
        $criteria->compare('gallery_id', (int)$id);
        $criteria->order = 'ordering';
        $photo = Photo::model()->find($criteria);
        if (!is_object($photo)) {
            $photo = new Photo;
            $photo->path = Yii::app()->themeManager->baseUrl . '/../uploads/images/product_placeholder_large.gif'; //place holder image
        }
        return $photo;
    }

    /**
     * @static return an array of all model photo passed by gallery ID
     * @param int $galleryId
     * @return array
     */
    public static function getListPhoto($galleryId,$excludeFirst=true)
    {
        if(!is_numeric($galleryId)){
            $galleryId=self::getTermIdByAlias($galleryId);
        }
        if ($galleryId == 0)
            return array();
        $criteria = new CDbCriteria;
        $criteria->compare('gallery_id', (int)$galleryId);
        $criteria->order = 'ordering';
        $photos = Photo::model()->findAll($criteria);
        //exclude the first image
        if($excludeFirst)
            unset($photos[0]);
        return $photos;
    }
    public static function getTermIdByAlias($alias){
        $voc=Vocabulary::model()->findByAttributes(array('alias'=>'gallery'));
        if($voc){
            $model=Term::model()->findByAttributes(array('alias'=>$alias,'v_id'=>$voc->id));
            if(!$model)
                return 0;
        }
        else return 0;
        return $model->id;


    }
}